/**
 * pager.js
 * @update		2010-11-3
 * @version		0.2
 */
(function($) {
	var a=function(d){var c=/^\d*$/;return c.test(d)};
	$.fn.pager=function(options) {
		function renderPager(options, obj){
			this.render = function() {
				if (options.pageNumber > options.pageCount) {
					//自动跳到第一页
					this.callBack(1);
					return;
				}
				var $pager = $('<ul></ul>');
				var _item=options.item;
				for(var i=0;i<_item.length;i++)
				{
					$pager.append(this.handler(_item[i]));
				}
				return $pager;
			}
			//render end
			this.handler=function(label){
				switch(label)
				{
					case "recordCount":
						var text=options.text["recordCount"];
						text=text.replace(/{#recordCount}/g,options.recordCount);
						return $('<li class="recordCount">' +text + '</li>');
					case "first":
					case "pre":
					case "next":
					case "last":
						return this.renderButton(label);
					case "pageCount":
						var text=options.text["pageCount"];
						text=text.replace(/{#pageCount}/g,options.pageCount);
						return $('<li class="pageCount">' +text + '</li>');
					case "qpage":
						return this.renderQPages();
					case "pageSizer":
						return this.renderPageSizer();
					case "quickPager":
						return this.renderQuickPager();
					default:
						return '<li class="text">'+label+'</li>';
				}
			}
			this.renderButton = function(buttonLabel) {
				var destPage = 1;
				var buttonText=options.text[buttonLabel];
				switch (buttonLabel) {
				case "first":
					destPage = 1;
					break;
				case "pre":
					destPage = options.pageNumber - 1;
					break;
				case "next":
					destPage = options.pageNumber + 1;
					break;
				case "last":
					destPage = options.pageCount;
					break;
				case "pageCount":
					destPage = options.pageCount;
					break;
				}
				var exp=new RegExp('{#'+buttonLabel+'}',"gi");
				buttonText=buttonText.replace(exp,destPage);
				var $Button = $('<li class="pgNext link '+buttonLabel+'">' + buttonText + '</li>');
				if (buttonLabel == "first" || buttonLabel == "pre") {
					options.pageNumber <= 1 ? $Button.addClass(buttonLabel+'-empty') : $Button.bind("click", {E: this},
					function(v) {
						v.data.E.callBack(destPage);
					});
				} else {
					options.pageNumber >= options.pageCount ? $Button.addClass(buttonLabel+'-empty') : $Button.bind("click", {E: this},
					function(v) {
						v.data.E.callBack(destPage);
					});
				}
				return $Button;
			}
			//renderButton end

			this.renderQPages = function() {
				var text=options.text["qpage"];
				var container=$("<span class='qpages'></span>");
				var startPoint = 1;
				var endPoint = options.qpageSize;
				var tmp=parseInt(endPoint/2);
				if (options.pageNumber > tmp) {
					startPoint = options.pageNumber - tmp;
					endPoint = options.pageNumber + tmp;
				}
				if (endPoint > options.pageCount) {
					startPoint = options.pageCount -tmp*2;
					endPoint = options.pageCount;
				}
				if (startPoint < 1) {
					startPoint = 1;
				} // loop thru visible pages and render buttons
				for (var page = startPoint; page <= endPoint; page++) {
					var currentButton = $('<li class="page-number link">' + text.replace(/{#qpage}/g,page) + '</li>');
					page == options.pageNumber ? currentButton.addClass('pgCurrent') : currentButton.bind("click", {
						E: this
					},
					function(v) {
						v.data.E.callBack(this.firstChild.data);
					});
					currentButton.appendTo(container);
				}
				if(options.pageCount>options.qpageSize){
					container.append('<li class="text">...</li>');
					var $Button = $('<li class="page-number link page-number-last">' + text.replace(/{#qpage}/g,options.pageCount) + '</li>');
					options.pageNumber >= options.pageCount ? $Button.addClass('pgEmpty') : $Button.bind("click", {E: this},
					function(v) {
						v.data.E.callBack(options.pageCount);
					});
					container.append($Button);
				}
				return container;
			} //renderQPages end

			this.renderQuickPager = function() {
				if(options.pageCount<=1)return null;
				var input=null;
				var t1 = $('<li class="text quickPager"></li>');
				if (options.pageCount <= 1) {//统一样式
					var html = '<select>';
					for (var i = 1; i <= options.pageCount; i++) {
						html += '<option value="' + i + '"';
						if (i == options.pageNumber) {
							html += ' selected';
						}
						html += '>' + i + '</option>';
					}
					html += '</select>';
					input = $(html);
					input.bind("change", {
						E: this
					},
					function(v) {
						v.data.E.callBack($(this).attr("value"));
					});
				} else {
					input = $('<div id="chatpage"><input id="quickPager" class="pagenum fl" value="' + options.pageNumber + '" style="width:'+(options.pageNumber.toString().length+1)*10+'px;"><a id="enter" class="enter fl" href="javascript:void(0)"></a></div><span class="fl">共&nbsp;/' + options.pageCount + '&nbsp;页</span>');
					input.find("#quickPager").bind("keypress", {E: this},function(e) {
						var E = e.data.E;
						if (e.keyCode == 13) {
							var p = $(this).attr("value");
							if (isNaN(p)) {
								alert("请输入数字！");
								return false;
							}
							if (parseInt(p) > options.pageCount) {
								alert("最大页数为" + options.pageCount + "！");
								return false;
							}
							if(parseInt(p) < 1){
								alert("页数不能为小于1的数！");
								return false;
							}
							E.callBack(p);
							return false;
						}
					});
					input.find("a#enter").bind("click", {E:this},function(e) {
						var E=e.data.E;
						var p = input.find("#quickPager").attr("value");
						if (!isNaN(p)) {
							alert("请输入数字！");
							return false;
						}
						if (parseInt(p) > options.pageCount) {
							alert("最大页数为" + options.pageCount + "！");
							return false;
						}
						E.callBack(p);
						return false;
					});
				}
				t1.append(input);
				return t1;
			}
			//renderQuickPage end

			this.renderPageSizer = function() {
				var t1;
				if (options.rowList) {
					var text=options.text["pageSizer"];
					text=text.replace(/{#pageSizer}/g,'</span><div id="select" class="fl"></div><span class="fl">');
					t1 = $('<li class="text pageSizer"><span class="fl">'+text+'</span></li>');
					var rowListHtml = '<select name="pageSize">';
					for (var i = 0; i < options.rowList.length; i++) {
						rowListHtml += '<option value="' + options.rowList[i] + '"';
						if (options.rowList[i] == options.pageSize) {
							rowListHtml += ' selected';
						}
						rowListHtml += '>' + options.rowList[i] + '</option>';
					}
					rowListHtml += "</select>";
					var input2 = $(rowListHtml);
					input2.bind("change", {
						E: this
					},
					function(v) {
						if(options.pageSize==this.value)return;
						options.pageSize=this.value;
						v.data.E.callBack(options.pageNumber);
					});
					$("#select", t1).append(input2);
				}
				return t1;
			}
			//renderPageSizer end
			this.callBack = function(page) {
				if(typeof(page)!="number")page=parseInt(page);
				if (page) options.pageNumber = page;
				if (typeof(options.callBack) == "function") options.callBack(options);
			}
			obj.empty().append(this.render());
		}

		var _default = {
			pageNumber: 1,
			pageCount: 1,
			pageSize:null,
			recordCount:0,
			qpageSize:9,
			rowList:null,//分页大小数组
			text:{//显示文字
					recordCount:"总数目:{#recordCount}",
					first:"首页",
					pre:"上一页",
					qpage:"{#qpage}",
					pageCount:"{#pageCount}",
					next:"下一页",
					last:"末页",
					pageSizer:'每页{#pageSizer}/行'
				},
			item:["recordCount","first","pre","qpage","next","quickPager"]//显示样式
		};
		
		options.text  = $.extend({}, _default.text, options.text);
		options  = $.extend({}, _default, options);
		return this.each(function() {
			new renderPager(options, $(this));
		});
	}
})(jQuery);
